<?php
/* SVN FILE: $Id: item_stealrandomitem.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Basic Steal Money item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/steal/item_stealrandomitem.php $
*/

class item_stealrandomitem extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Steal Random Item";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Use this item to steal one item from people.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['protected'] = array($this->settings['admin_group']);
			$this->itemSettings['allowed_item'] = array_keys($this->caches['market_items']);
			$this->itemSettings['most_items'] = 1;
			$this->itemSettings['percent'] = 50;
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$items = array();
		foreach($this->caches['market_items'] as $item_id => $item)
		{
			$items[] = array($item['item_id'], $item['item_name']);
		}
		
		$form['protected'] = ipsRegistry::getClass('output')->generateGroupDropdown('item[protected][]', $this->itemSettings['protected'], true);
		$form['allowed_item'] = ipsRegistry::getClass('output')->formMultiDropdown('item[allowed_item][]', $items, $this->itemSettings['allowed_item']);
		$form['most_items'] = ipsRegistry::getClass('output')->formSimpleInput('item[amount_max]', $this->itemSettings['most_items']);
		$form['percent'] = ipsRegistry::getClass('output')->formSimpleInput('item[percent]', $this->itemSettings['percent']);
		return <<<EOF
				<li>
					<label>{$this->lang->words['groups_protected']}</label>
					{$form['protected']}
				</li>
				<li>
					<label>{$this->lang->words['steal_which_item']}</label>
					{$form['allowed_item']}
				</li>
				<li>
					<label>{$this->lang->words['steal_item_amount']}</label>
					{$form['most_items']}
				</li>
				<li>
					<label>{$this->lang->words['steal_percent']}</label>
					{$form['percent']}%
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['protected'] = is_array($input['protected']) ? $input['protected'] : array();
		$setting['allowed_item'] = is_array($input['allowed_item']) ? $input['allowed_item'] : array();
		$setting['most_items'] = numbers::max(numbers::parseInt($input['most_items']),1);
		$setting['percent'] = numbers::parseFloat($input['percent']);
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->request['request_method'] == 'post')
		{
			$member = IPSMember::load($this->request['member_name'],'all','displayname');
			if(!$member)
			{
				$this->registry->getClass('output')->showError('no_member');
			}
			
			if(in_array($member['member_group_id'],$this->itemSettings['protected']))
			{
				$this->registry->getClass('output')->showError('protected_member');
			}
			
			$inventory = marketRegistry::getInventoryItem($member['member_id']);
			$invItems = array();
			foreach($inventory as $_invItem)
			{
				if(in_array($_invItem['item'],$this->itemSettings['allowed_item']))
				{
					$invItems[] = $_invItem;
				}
			}
			
			$rand = numbers::rand(0,100,1);
			$partSuccess = count($invItems) > $this->itemSettings['most_items'];
			
			$this->deleteItems($invItem);
			
			if($partSuccess && numbers::compare($rand, $this->itemSettings['percent']) < 0)
			{
				$keys = array_rand($invItems, $this->itemSettings['most_items']);
				if($this->itemSettings['most_items'] == 1)
				{
					$keys = array($keys);
				}
				$invIDs = array();
				foreach($keys as $key)
				{
					$invIDs[] = $invItems[$key]['i_id'];
				}
				
				$this->DB->update('market_inventory',array('member_id'=>$this->memberData['member_id']),"i_id IN (".implode(", ",$invIDs).")");
				
				$this->writeLog($invItem, "Successfully stole some random items from <a href='{$this->settings['base_url']}showuser={$member['member_id']}'>{$member['members_display_name']}</a>");
				
				$this->registry->getClass('output')->redirectScreen( $this->lang->words['steal_successful'], $this->settings['base_url_with_app'] . "module=inventory" );
			}
			else
			{
				$this->writeLog($invItem, "Failed stealing some random items from <a href='{$this->settings['base_url']}showuser={$member['member_id']}'>{$member['members_display_name']}</a>");
				
				$this->registry->getClass('output')->redirectScreen( $this->lang->words['steal_failed'], $this->settings['base_url_with_app'] . "module=inventory" );
			}
		}
		
		return <<<EOF
<form id='stealItemForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='stealItem'class='block_wrap'>
		<h3 id='steaMoneyHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['which_member']}</label>
					<input type='text' name='member_name' value='' size='32' id='member_name' autocomplete='off' class='input_text' />
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['steal_submit']}" name="submit" />
		</fieldset>
	</div>
</form>
<script type='text/javascript'>
if( $('member_name') )
{
	new ipb.Autocomplete( $('member_name'), { multibox: false, url: ipb.vars['base_url'] + 'app=core&module=ajax&section=findnames&do=get-member-names&secure_key={$this->member->form_hash}&name=', templates: { wrap: ipb.templates['autocomplete_wrap'], item: ipb.templates['autocomplete_item'] } } );
}
</script>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
